package com.none.web.QRcode.coder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.none.web.QRcode.QRCodeGenerateTest;
import com.none.web.QRcode.constants.AppConstants;
import com.none.web.QRcode.util.AESUtil;
import com.none.web.QRcode.util.QRCodeUtil;

public class NewUserSignUpQRCoder {

	/**
	 * encrypt
	 * 
	 * @param staffId
	 * @param entity
	 * @param department
	 * @param mail
	 * @param chineseName
	 * @param key
	 * @return
	 */
	public static String encrypt(String staffId, String entity,
			String department, String mail, String chineseName, String key) {
		String actionType = AppConstants.ACTIONTYPE_NEWUSERSIGNUP;
		String content = staffId + AppConstants.QRCODER_SPLIT_CHAR + entity
				+ AppConstants.QRCODER_SPLIT_CHAR + department
				+ AppConstants.QRCODER_SPLIT_CHAR + mail
				+ AppConstants.QRCODER_SPLIT_CHAR + chineseName
				+ AppConstants.QRCODER_SPLIT_CHAR + actionType;
		return AESUtil.encrypt(content, key);
	}

	/**
	 * 
	 * @param code
	 * @param String
	 *            []{staffId,entity,department,mail,chineseName,actionType}
	 * @return
	 */
	public static String[] decrypt(String code, String key) {
		String content = AESUtil.decrypt(code, key);
		String[] values = content.split(AppConstants.QRCODER_SPLIT_CHAR_ESCAPE);
		// check
		if (values.length != 6) {
			// TODO: error message
		}
		return values;
	}
	
	// DEMO
	public static void main(String[] args) throws WriterException, IOException,
			NotFoundException {
		String staffId = "25145862";
		String entity = "GLTc";
		String department = "Mobile_GZ";
		String mail = "kennethwkwong@hsbc.com.hk";
		String chineseName = "kennethwkwong";
		
		String fileName = "kennethwkwong";
		
		// get statick key
		String staticKey = AppConstants.QR_CODER_AES_STATIC_KEY;
		
		String content = NewUserSignUpQRCoder.encrypt(staffId, entity, department, mail, chineseName,staticKey);
		
		File file = null;
		FileOutputStream fos = null;

		// generateNewUserSignUpQRCode
		System.out.println("NewUserSignUpQRCode:"+content);
		file = new File("/Users/yanjun/Documents/workspace/JavaWS/tmp/NewUserSignUpQRCode-"+fileName+".png");
		fos = new FileOutputStream(file);
		QRCodeUtil.encode(content, fos);
		fos.close();

		// generateForgetAppPasswordQRCode
		content = ForgetAppPasswordQRCoder.encrypt(staffId, entity,
				department, mail, chineseName, staticKey);
		System.out.println("ForgetAppPasswordQRCoder:"+content);
		file = new File("/Users/yanjun/Documents/workspace/JavaWS/tmp/ForgetAppPasswordQRCode-"+fileName+".png");
		fos = new FileOutputStream(file);
		QRCodeUtil.encode(content, fos);
		fos.close();
		
		// generateDeviceChangeQRCode
		content = DeviceChangeQRCoder.encrypt(staffId, entity,
				department, mail, chineseName, staticKey);
		System.out.println("DeviceChangeQRCoder:"+content);
		file = new File("/Users/yanjun/Documents/workspace/JavaWS/tmp/DeviceChangeQRCoder-"+fileName+".png");
		fos = new FileOutputStream(file);
		QRCodeUtil.encode(content, fos);
		fos.close();
	}

}
